<?php
namespace app\taiyuan\controller;
use think\captcha\Captcha; //验证码
use think\Db;
class Publics extends Base
{
	public function _initialize()
	{
		// 调用父类前置方法
		parent::_initialize();
	}

	/**
	 * 左侧菜单
	 */
	public function navs()
	{
		$result = [
			[
				'id' => 0,
				'title' => '工作空间',
				'icon' => 'layui-icon layui-icon-console',
				'type' => 1,
				'href' => url('Index/welcome'),
			],
            [
                'id' => 'showinfo',
                'title' => '数据明细',
                'icon' => 'layui-icon layui-icon-console',
                'type' => 1,
                'href' => url('Index/show'),
            ],


		];
		echo json_encode($result);
	}

	/**
	 * 验证码
	 */
	public function captcha()
	{
	    $config =    [
            // 验证码位数
            'length'      =>    4,   
           
        ];
		$captcha = new Captcha($config);
		$captcha->codeSet = '0123456789'; 
		return $captcha->entry();
	}

	/**
	 * 用户登录
	 */
	public function login()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			$params = input(); //接收post参数
			$validate = $this->validate(
				$params,
				[
					'username' => 'require',
					'password' => 'require',
					'captcha'  => 'require'
				],
				[
					'username.require' => '请填写用户名',
					'password.require' => '请填写密码',
					'captcha.require'  => '请填写验证码',
				]
			);
			if(true !== $validate)
			{
				echo json_encode(['code' => -1, 'msg' => $validate]);
				exit();
			}
			//验证验证码是否正确
			$captcha = new Captcha();
			if(!$captcha->check($params['captcha']))
			{
				echo json_encode(['code' => -1, 'msg' => '验证码不正确']);
				exit();
			}
			//查询用户
			$info = Db::name('proxy')->where(['user_name' => $params['username']])->find();
			//判断是否有该用户
			if(empty($info))
			{
			   	echo json_encode(['code' => -1, 'msg' => '用户不存在']);
			   	exit();
			}
			//判断密码是否正确
			if($info['password'] != CheckMd5($params['password'],$info['code']))
			{
				echo json_encode(['code' => -1, 'msg' => '密码不正确']);
				exit();
			}
			//校验成功
			//session存储
			$proxy['proxy_id']       = $info['proxy_id'];
            $proxy['user_name'] = $info['user_name'];
            $proxy['is_xian'] = $info['is_xian'];

			session('proxy', $proxy);
			// 返回数据,更新数据库
			$session_data = session('proxy');

			if(!empty($session_data))
			{
				$result['code'] = 0;
				$result['msg'] = "登录成功";
			}else
			{
				// 失败
				session('proxy', null);
				$result['code'] = -1;
				$result['msg'] = "登录失败";
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}

	/**
	 * 退出登录
	 */
	public function logout()
	{
		session('proxy', null);
		$this->redirect('Publics/login');
	}

	/**
	 * 修改密码
	 */
	public function edit_password()
	{
		$result['code'] = -1;
		$result['msg'] = "非法请求";
		if(request()->isPost())
		{
			//登录校验
			$this->IsLogin();
			$params = input(); //接收post参数
			$params['proxy'] = $this->proxy;
			//dump($params);
			$validate = $this->validate(
				$params,
				[
					'oldpassword' => 'require',
					'newpassword' => ['require', 'regex' => '/^(?=.*\d)(?=.*[a-z])(?=.*[!-\/:-@\[-`{-~])[!-~]{8,20}$/i'],
					'respassword' => 'require|confirm:newpassword'
				],
				[
					'oldpassword.require' => '请填写原始密码',
					'newpassword.require' => '请填写新密码',
					'newpassword.regex'   => '新密码必须为8-20位并包含字母、数字、特殊符号',
					'respassword.require' => '请填写确认密码',
					'respassword.confirm' => '新密码与确认密码不一致'
				]
			);
			if(true !== $validate)
			{
				echo json_encode(['code' => -1, 'msg' => $validate]);
				exit();
			}
			//查询用户是否存
			$info = Db::name('proxy')->where(['proxy_id' => $params['proxy']['proxy_id']])->find();
			if(empty($info))
			{
				echo json_encode(['code' => -1, 'msg' => '该用户不存在']);
				exit();
			}
			//判断原始密码是否正确
			if($info['password'] != CheckMd5($params['oldpassword'],$info['code']))
			{
				echo json_encode(['code' => -1, 'msg' => '原始密码不正确']);
				exit();
			}
			$data['code']     = GetRandomStr(6);
			$data['password'] = CheckMd5($params['newpassword'],$data['code']);
			$data['upd_time'] = time();
			if(Db::name('proxy')->where(['proxy_id' => $info['proxy_id']])->update($data))
			{
				session('prxoy', null);
				$result['code'] = 0;
				$result['msg'] = "密码修改成功";
			}else
			{
				$result['code'] = -1;
				$result['msg'] = "密码修改失败";
			}
			echo json_encode($result);
		}else
		{
			return $this->fetch();
		}
	}
}